package mysql

import (
	"go.uber.org/zap"
	"struggle/model"
)

type imageDAO struct{}

var DefaultImage = imageDAO{}

func (m imageDAO) Create(image *model.Image) (err error) {
	sqlStr := `INSERT INTO st_image (md5, path, size, width, height) VALUES (?, ?, ?, ?, ?)`
	_, err = db.Exec(sqlStr, image.Md5, image.Path, image.Size, image.Width, image.Height)
	if err != nil {
		zap.L().Error("crate image failed", zap.Any("image", image), zap.Error(err))
		return
	}
	return
}

func (m imageDAO) FindOne(md5 string) (image *model.Image, err error) {
	image = new(model.Image)
	sqlStr := `SELECT * FROM st_image WHERE md5=?`
	err = db.Get(image, sqlStr, md5)
	if err != nil {
		zap.L().Error("get image failed", zap.Error(err))
		return
	}
	return
}
